package psa;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;

import org.unt.genlogic.Files;

public class FindFilesDirsRecurs {

	// search for @Mojo(name = "report")
	public static void main(String[] args) {

		String search = "sourceEncoding".toLowerCase(); // @Mojo @goal

		List<File> filesList = new ArrayList<File>();
		Set<String> extensions = new HashSet<String>();
		extensions.add("html");
		Files.listFilesRecursively(new File("C:/User/U336654/jip-data/sitePublish/ab100"), filesList, extensions);
		System.out.println("Amount of files " + filesList.size() + "...");

		for (File file : filesList) {
			String fileString = Files.readFileAsString(file);
			System.out.println("Checking " + file.getAbsolutePath());
			if (fileString.toLowerCase().indexOf(search) > -1) {
				String str = fileString.substring(fileString.indexOf(search) + search.length());
				System.out.println(search + str.substring(0, str.indexOf(" ") - 1) + "\t\t" + file.getName());
			} else {
				// System.out.println("\t\tPAS");
			}
		}
	}

	public static void amain(String[] args) {

		List<File> files = new ArrayList<File>();

		Files.listFilesRecursively(new File("C:/IDVS"), files);

		SortedMap<Long, File> sortedMap = new TreeMap<Long, File>();

		for (File file : files) {
			sortedMap.put(file.length(), file);
		}

		Collection<File> files2 = sortedMap.values();
		for (File file2 : files2) {
			System.out.println(file2.length() + "\t" + file2.getName().substring(file2.getName().indexOf(".") + 1) + "\t" + file2.getName() + "\t"
					+ file2.getAbsolutePath());
		}

		System.out.println(sortedMap.firstKey());
		System.out.println(sortedMap.lastKey());
		System.out.println("Total files " + files.size());
	}

	public static void mmm() {

		List<File> files = new ArrayList<File>();
		Set<String> set = new HashSet<String>();
		set.add("apt");

		Files.listFilesRecursively(new File("C:/IDVS/workspace2/jip-site"), files, set);

		for (File file : files) {
			String fileContents = Files.readFileAsString(file);
			fileContents = fileContents.toLowerCase();
			if (fileContents.indexOf("205206652") > -1) {
				System.out.println(file.getName() + "\t" + file.getAbsolutePath());
			}
		}
	}

	public static void cvs_svn(String[] args) {

		List<File> files = new ArrayList<File>();
		Set<String> set = new HashSet<String>();
		set.add("apt");

		Files.listFilesRecursively(new File("C:/IDVS/workspace2/jip-site"), files, set);

		for (File file : files) {
			String fileContents = Files.readFileAsString(file);
			fileContents = fileContents.toLowerCase();
			if (fileContents.indexOf("cvs") > -1) {
				int countCVS = fileContents.split("cvs").length - 1;
				int countCVSSVN = fileContents.split("cvs/svn").length - 1;
				if (countCVSSVN != countCVS)
					System.out.println(countCVSSVN + "\t" + (Math.abs(countCVSSVN - countCVS)) + "\t" + file.getName() + "\t"
							+ file.getAbsolutePath());
			}
		}
	}
}
